﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script type="text/javascript" language="javascript">
        function Call() {
         
            $("#divMessagePanel").html("");
            var formativeData = { "BoolValue": 1, "StringValue": "李四" };
            var jsonStr = '{"composite":' + json2string(formativeData) + "}";

            $.ajax({
                type: "post",
                url: "http://localhost:9162/Service1.svc/GetDataUsingDataContract",
                contentType: "application/json;charset=utf-8",
                data: jsonStr,
                success: function(data) {
                    alert(data);
                    var a = eval(data);

                    alert(a.GetDataUsingDataContractResult.StringValue);
                    $('#divMessagePanel').html("ok");
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    $("#divMessagePanel").html("error");
                },
                cache: false
            });
        }

        function CallList() {
            $("#divMessagePanel").html("");
            var formativeData = { ct1: { "BoolValue": 1, "StringValue": "李四" }, ct2: { "BoolValue": 1, "StringValue": "张三"} };
            var jsonStr = json2string(formativeData);
            

            $.ajax({
                type: "post",
                url: "http://localhost:9162/Service1.svc/DataContractList",
                contentType: "application/json;charset=utf-8",
                data: jsonStr,
                success: function(data) {
                    alert(data);
                    var a = eval (data ); // JSONX.parse 有问题

                    alert(a[0].StringValue);
                    $('#divMessagePanel').html("ok");
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    $("#divMessagePanel").html("error");
                },
                cache: false
            });
        }
       
        //可选，采用 json2.js
        function json2string(strObject) {
            var c, i, l, s = '', v, p;

            switch (typeof strObject) {
                case 'object':
                    if (strObject) {
                        if (strObject.length && typeof strObject.length == 'number') {
                            for (i = 0; i < strObject.length; ++i) {
                                v = json2string(strObject[i]);
                                if (s) {
                                    s += ',';
                                }
                                s += v;
                            }
                            return '[' + s + ']';
                        } else if (typeof strObject.toString != 'undefined') {
                            for (i in strObject) {
                                v = strObject[i];
                                if (typeof v != 'undefined' && typeof v != 'function') {
                                    v = json2string(v);
                                    if (s) {
                                        s += ',';
                                    }
                                    s += json2string(i) + ':' + v;
                                }
                            }
                            return '{' + s + '}';
                        }
                    }
                    return 'null';
                case 'number':
                    return isFinite(strObject) ? String(strObject) : 'null';
                case 'string':
                    l = strObject.length;
                    s = '"';
                    for (i = 0; i < l; i += 1) {
                        c = strObject.charAt(i);
                        if (c >= ' ') {
                            if (c == '\\' || c == '"') {
                                s += '\\';
                            }
                            s += c;
                        } else {
                            switch (c) {
                                case '\b':
                                    s += '\\b';
                                    break;
                                case '\f':
                                    s += '\\f';
                                    break;
                                case '\n':
                                    s += '\\n';
                                    break;
                                case '\r':
                                    s += '\\r';
                                    break;
                                case '\t':
                                    s += '\\t';
                                    break;
                                default:
                                    c = c.charCodeAt();
                                    s += '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
                            }
                        }
                    }
                    return s + '"';
                case 'boolean':
                    return String(strObject);
                default:
                    return 'null';
            }
        }    　
    </script>

</head>
<body>
    <form id="form1" runat="server">

    <script src="Script/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>



    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" 
            Text="复合类型请求测试" />
        <br />
        <h1 class="title_txt">
            <a href="http://blog.csdn.net/zfrong/archive/2009/05/14/4183885.aspx">&nbsp; jquery使用json格式
                调用 restful wcf web service</a></h1>
        <br />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    <div id="divMessagePanel">
        <input type="button" value="Call" onclick="Call()" />
        <input type="button" value="CallList" onclick="CallList()" />
    </div>
    服务器端处理数据&nbsp; <asp:Button ID="Button2" runat="server" Text="测试" 
        onclick="Button2_Click" />
    <br />
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
    
    <br />
    </form>
</body>
</html>
